import { getNowWeek } from '../../utils/util';

Page({
  data: {
    navList: [
      {
        title: '查课表',
        icon: '/asset/imgs/course.png',
        path: '/pages/course/index'
      },
      {
        title: '查成绩',
        icon: '/asset/imgs/score.png',
        path: '/pages/score/index'
      },
      {
        title: '查考勤',
        icon: '/asset/imgs/attendance.png',
        path: '/pages/attendance/index'
      },
      {
        title: '校历',
        icon: '/asset/imgs/calendar.png',
        path: '/pages/calendar/index'
      },
    ],
    startDate: '2024/09/20', // 开学日期
    totalWeek: 20,
    todayCourseList: [],
  },

  onLoad(){
    this.getTodayCourseList()
  },
  getTodayCourseList() {
    const todayWeek = new Date().getDay()  // 周日0
    // 用于计算当前日期相对于一个开始日期的星期数，并且在总周数达到后重置到 1
    const todayWeeks = getNowWeek(this.data.startDate, this.data.totalWeek)
    // 数据来源pages\course\index.js
    const courseList = wx.getStorageSync('courses')
    const todayCourseList = courseList.filter(item => {
      // item.week == todayWeek: 检查课程的week属性是否等于当前星期（todayWeek） 
      // item.weeks.indexOf(todayWeeks) > -1: 检查item.weeks数组中是否包含当前周数（todayWeeks），indexOf返回-1表示在数组中未找到匹配的周数
      return item.week == todayWeek && item.weeks.indexOf(todayWeeks) > -1
    })
    todayCourseList.sort((a,b) => {
      return a.section - b.section
    })

    this.setData({
      todayWeek,
      todayWeeks,
      todayCourseList,
    })
  },

  // 跳转
  nav(e){
    // 自定义属性 data-index="{{index}}"
    const index = e.currentTarget.dataset.index
    const path = this.data.navList[index].path
    wx.navigateTo({
      url: path,
      fail(){ 
        wx.switchTab({ // wx.navigateTo只能跳转到非tabBar页面，如果跳转到tabBar页面会失败，tabBar页面必须使用wx.switchTab来打开
          url: path,
        })
      }  //fail
    })
  },
  
})
